﻿using UnityEngine;
using QFramework;
using System.IO;
using System;
using Newtonsoft.Json;
using System.Collections.Generic;
namespace HareGame
{
    public partial class Util
    {
        #region StreamingAssets
        public string GetStreamingAssetsPath(string filePath)
        {
            string path = "";
#if UNITY_ANDROID && !UNITY_EDITOR
            path = Path.Combine(Application.streamingAssetsPath, filePath);
#else
            path = Path.Combine(@"file:///" + Application.streamingAssetsPath, filePath);
#endif
            return path;
        }

        private string __ReadFromStreamAssets(string jsonFilePath)
        {
            var fullPath = GetStreamingAssetsPath(jsonFilePath);
            WWW www = new WWW(fullPath);
            while (!www.isDone) { }
            return www.text;
        }

        public T ReadFromStreamAssets<T>(string jsonFilePath)
        {
            return JsonUtility.FromJson<T>(__ReadFromStreamAssets(jsonFilePath));
        }

        public object ReadFromStreamAssets(string jsonFilePath, Type type)
        {
            return JsonUtility.FromJson(__ReadFromStreamAssets(jsonFilePath), type);
        }

        public List<T> ReadJsonDataFromStreamAssets<T>(string jsonName)
        {
            return JsonConvert.DeserializeObject<List<T>>(__ReadFromStreamAssets("JsonData/" + jsonName + ".json"));
        }
        #endregion
    }
}